package com.zat.mes.andrmes.dofun.main.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.TodoBean
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import retrofit2.Call
import retrofit2.Response

/**
 * 待办事项
 */
class TodoModel(application: Application) : AndroidViewModel(application) {

    private val todoData: MutableLiveData<BaseResponse<List<TodoBean>>> = MutableLiveData()
    private val todoDataFail: MutableLiveData<String> = MutableLiveData()

    fun getTodoList(): LiveData<BaseResponse<List<TodoBean>>> {
        return todoData
    }

    fun getTodoFail(): LiveData<String> {
        return todoDataFail
    }

    /**
     * 获取待办事项
     */
    fun loadTodoList(curPage: Int, pageSize: Int, empId: Long, status: String) {


        RetrofitManager.apiService(ApiService::class.java)
            .getTodoListNew(curPage, pageSize, empId, status)
            .enqueue(object : MesCallBack<BaseResponse<List<TodoBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<TodoBean>>>?,
                    response: Response<BaseResponse<List<TodoBean>>>?
                ) {
                    todoData.postValue(response?.body())
                }

                override fun onFailure(call: Call<BaseResponse<List<TodoBean>>>, t: Throwable) {
                    super.onFailure(call, t)
                    todoDataFail.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                }

            })
    }

}